// pages/a1/a1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:'',
    buttons:[{id:1,name:'男'},{id:0,name:'女'},{id:null,name:'你猜'}],
    newuser:'',
    sexname:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({user:getApp().globalData.user,newuser:getApp().globalData.user})
    let date=new Date(this.data.user.birthady)
    let newdate=date.getFullYear()+'-'+Number(date.getMonth()+1)+'-'+date.getDate()
    this.setData({'user.birthady':newdate})
    this.setData({newuser:this.data.user})
    if(this.data.newuser.sex===1){
      this.setData({
        sexname:'男'
      })
    }
    else if(this.data.newuser.sex===0){
      this.setData({
        sexname:'女'
      })
    }
    else{
      this.setData({
        sexname:'你猜'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  changedate(e){
    this.setData({'newuser.birthady':e.detail.value})
  },
  changename(e){
    this.setData({'newuser.username':e.detail.value})
  },
  changebrief(e){
    this.setData({'newuser.brief':e.detail.value})
  },
  changesex(e){
    this.setData({'newuser.sex':e.target.id})
    console.log(this.data.newuser)
  },
  changephone(e){
    this.setData({'newuser.phone':e.detail.value})
  },
  submit(){
    wx.request({
      url: 'http://localhost:3000/user/change',
      data:{
        "id":this.data.newuser.id,
        "name":this.data.newuser.username,
        "sex":this.data.newuser.sex,
        "brief":this.data.newuser.brief,
        "phone":this.data.newuser.phone,
        "birthday":this.data.newuser.birthady
      },
      method: 'GET',
      success:res=>{
        getApp().globalData.user=this.data.newuser
        console.log(getApp().globalData.user)
        wx.switchTab({
          url: '/pages/mine/mine',
        })
      },
      fail:err=>{
        console.log(err)
      }
    })
  }
})